package com.chencs.coupon;

import com.alibaba.fastjson.JSON;
import com.chencs.coupon.hwc.HWCCoupons;
import com.chencs.coupon.hwc.HWCRootBean;
import com.chencs.coupon.hwc.HWCUtils;
import com.chencs.coupon.wk.EffectiveBean;
import com.chencs.coupon.wk.WKUtils;
import com.chencs.vo.biz.password.ApplyCommission;
import com.chencs.vo.biz.password.QinTao;
import com.chencs.vo.biz.product.ProductModel;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class CouponUtils {

    public static String counp = "https://uland.taobao.com/coupon/edetail?e=";
    public static List<QinTao> qinTaoList;
    public static ProductModel model;
    public static ProductModel getCoupon(String code){
        HWCRootBean hwcRootBean =  HWCUtils.getHWCData(code);
        if(hwcRootBean.getSuccess()){//慧万村的拿成功
            ApplyCommission applyCommission =  WKUtils.getCouponID(hwcRootBean.getProduct().getOpen_id()+"","");
            EffectiveBean effective =  WKUtils.geEffective(applyCommission.getData().getCoupon_click_url());
         // System.out.println(JSON.toJSONString(effective));
            qinTaoList = new ArrayList<>();
             model = new ProductModel();
           if("0".equals(effective.getData().getRetStatus())){
               QinTao qinTao1 = new QinTao();
               qinTao1.setUrl(applyCommission.getData().getCoupon_click_url());
               qinTao1.setInfo(applyCommission.getData().getCoupon_info()+"(内部券)");
               qinTao1.setPrice(effective.getData().getAmount()+"");
               qinTaoList.add(qinTao1);
           }
            model.setProimg("http:"+effective.getData().getItem().getPicUrl());
            model.setPname(effective.getData().getItem().getTitle());
            model.setPrice(hwcRootBean.getProduct().getPrice()+"");
//            model.setCounpurl(applyCommission.getData().getCoupon_click_url());
            model.setProurl("http:"+effective.getData().getItem().getClickUrl());

            QinTao qinTao2 = null;
            List<HWCCoupons> coupons = hwcRootBean.getProduct().getCoupons();
           if(!CollectionUtils.isEmpty(coupons)){
               for(int i =0;i<coupons.size();i++){
                   if(!StringUtils.isEmpty(coupons.get(i).getActivity_id())){
                       qinTao2 = new QinTao();
                       qinTao2.setInfo(coupons.get(i).getApplyAmountTxt()+"元");
                       qinTao2.setUrl(counp+effective.getEstr()+"&activityId="+coupons.get(i).getActivity_id());
                       qinTao2.setPrice(coupons.get(i).getAmount()+"");
                       qinTaoList.add(qinTao2);
                   }
               }
           }
            model.setList(qinTaoList);
//           System.out.println(JSON.toJSONString(model));
           return  model;
        }else{
            System.out.println("没数据");
            return null;
        }




    }

//    public static void main(String[] args) {
//     String code = "44CQ5bm/6KW/55m+6aaZ5p6c5rC05p6c546w5pGY546w5Y+R57K+6KOFNeaWpOWkp+e6ouaenOmFuOmmmeaWsOmynOawtOaenOilv+eVquiOsum4oeibi+aenOOAkWh0dHA6Ly9tLnRiLmNuL2guV0J6OGtLdSDngrnlh7vpk77mjqXvvIzlho3pgInmi6nmtY/op4jlmajmiZPlvIDvvJvmiJblpI3liLbov5nmnaHkv6Hmga/vv6V3VlRpMHVLbVN3Nu+/peWQjuaJk+W8gOaJi+a3mA==";
//      CouponUtils.getCoupon(code);
//
//    }
}
